<?php

class Model_article extends CI_Model
{
	function __construct()
	{
	    parent::__construct();	
	}
	
	public function save()
	{
		$article = array();
		$article = $this->input->post();
		$tag = $this->input->post('tags');
		$tag_array = explode('，',$tag);
		$article['id'] = NULL;
		$article['created'] = time();
		$article['own'] = "伟大的L";
		$article['is_show'] = 1;
		$this->db->insert('articles',$article);
		$insert_id = $this->db->insert_id();
		
		foreach($tag_array as $v)
		{
			$this->db->insert('tag', array('id'=>$insert_id,'name'=>$v));
		}
	}
	
	public function get_one($id)
	{
		$this->load->database();
		$id = intval($id);
		$this->db->select('*');
		$this->db->where('id',$id,FALSE);
		$article = $this->db->get('articles');
		$data = $article->result_array();
		return $data[0];
	}
	
	public function update($post)
	{
		$this->load->database();
		$tag = $this->input->post('tags');
		$tag_array = explode('，',$tag);
		$id = $post['id'];
		$id = intval($id);
		$this->db->where('id',$id);
		$post = array_splice($post,0,count($post));  //删除ID字段，不然无法更新  -_-!
		$post['created'] = time();
		$this->db->update('articles',$post);
	    foreach($tag_array as $v)
		{
			$this->db->insert('tag', array('id'=>$id,'name'=>$v));
		}
	}
}